if(window.cache==undefined) {
	window.cache = {};
}

//浏览器控制
var UA = navigator.userAgent.toLowerCase();
if(UA.indexOf("chrome") == -1) {
	window.location = "../sys/browser.do";
}

loadSkin();
loadFontSize();

$(function() {
	//选择器为只读
	$(".z_datetime, .z_date, [data_tree]").each(function(){
		$(this).attr("readonly","readonly");
		//$(this).css("backgroundColor","#fff");
	});

	//自动初始化日期选择器
	$(".z_datetime").click(function(){WdatePicker({dateFmt:"yyyy-MM-dd HH:mm:ss"});});
	$(".z_date").click(function(){WdatePicker({dateFmt:"yyyy-MM-dd"});});
	
	$("body").bind("click", function() {
		if((typeof hideMenu) == "undefined" && (typeof top.hideMenu) == "function") {
			top.hideMenu();
		}
	});
});

//日期格式化
Date.prototype.format = function(fmt) {
	if(fmt == null) {
		fmt = "yyyy-MM-dd HH:mm:ss";
	}
	var o = {
		"M+" : this.getMonth()+1,//月份 
		"d+" : this.getDate(),//日 
		"H+" : this.getHours(),//小时 
		"m+" : this.getMinutes(),//分 
		"s+" : this.getSeconds()//秒 
	};
	if(/(y+)/.test(fmt)) {
			fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	}
	for(var k in o) {
		if(new RegExp("("+ k +")").test(fmt)){
			 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
		 }
	}
	return fmt; 
}

//加载皮肤
function loadSkin() {
	var skin = tool.getCookie("skin");
	if(!skin) {
		skin = "light";
	}
	document.write('<link  href="../static/ui/myZui/'+skin+'.css" rel="stylesheet">');
}

//加载字体
function loadFontSize() {
	var fontSize = tool.getCookie("fontSize");
	
	if(!fontSize) {
		fontSize = "13";
	}
	
	var h = ''+
		'<style type="text/css">'+
		'	#z_body{font-size:'+fontSize+'px;}'+
		'</style>';
	document.write(h);
}

//关闭弹窗
function closeModal() {
	$("#triggerModal .close").click();
}

//刷新页面
function refresh() {
	window.location = window.location.href;
}


//表单验证
var validateRegs = {
	notEmpty: {tip:"必填项"},
	number: {reg:/^[0-9][.0-9]*$/, tip:"请输数字"},
	mobile: {reg:/^[1][0-9]{10}$/, tip:"手机号格式有误"},
	jwthm: {reg:/^[1][0-9]{10}$/, tip:"格式有误"},
	max999: {reg:/^[1-9][0-9]{1,2}$/, tip:"必须是小于999的整数"},
	max99999: {reg:/^[0-9]{1,5}$/, tip:"必须是小于99999的整数"},
	num6: {reg:/^[0-9]{6}$/, tip:"必须6位数字"},
	num12_A: {reg:/^[0-9A-Z]{12}$/, tip:"必须12位数字与大写字母组合"},
	tel: {reg:/^(0\d{2,3}-\d{5,9})|([1][0-9]{10})$/, tip:"电话格式有误"},
	ip: {reg:/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/, tip:"IP地址格式有误"},
	email: {reg:/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ , tip:"邮箱地址格式有误"},
	pwd: {reg:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/ , tip:"密码需6至12位字母与数字组合"}
};
function validateForm(formId) {
	var res = true;
	$("#"+formId+" [validType]").each(function() {
		var val_ = $(this).val();
		var val = (val_ == null) ? "" : val_.trim();
		if(val != val_) {
			$(this).val(val);
		}
		
		var strTypes = $(this).attr("validType");
		if(strTypes.length > 0) {
			var arr = strTypes.split(",");
			var valid = true;
			for(var i=0; i<arr.length; i++) {
				var type = arr[i];
				if(type == "notEmpty") {
					if(val == "") {
						valid = false;
						onUnvalid(this, "必填项");
						break;
					}
				} else {
					if(val != "" && !validateRegs[type].reg.test(val)) {
						valid = false;
						onUnvalid(this, validateRegs[type].tip);
						break;
					}
				}
			}
			
			if(valid) {
				onValid(this);
			} else {
				res = false;
			}
		}
	});
	return res;
}
function onUnvalid(dom, tip) {
	var $ipt = $(dom);
	if($ipt.siblings(".unvalid").length == 0) {
		$ipt.after('<div class="unvalid" style="color:red; text-align:right;"></div>');
	}
	$ipt.siblings(".unvalid").html(tip);
}
function onValid(dom) {
	$(dom).siblings(".unvalid").hide();
}


//分页
function firstPage() {//首页
	vm.pageNum = 1;
	getPage();
}
function lastPage() {//尾页
	vm.pageNum = vm.pages;
	getPage();
}
function prevPage() {//上一页
	if(vm.pageNum > 1) {
		vm.pageNum --;
		getPage();
	}
}
function nextPage() {//下一页
	if(vm.pageNum < vm.pages) {
		vm.pageNum ++;
		getPage();
	}
}